package cn.itsource.hrm.fallback;

import cn.itsource.hrm.doc.ESBaseQuery;
import cn.itsource.hrm.doc.CourseDoc;
import cn.itsource.hrm.feignclients.ESFeignClient;
import cn.itsource.hrm.query.CourseQuery;
import cn.itsource.hrm.util.AjaxResult;
import cn.itsource.hrm.util.PageList;
import feign.hystrix.FallbackFactory;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestBody;

@Component
public class ESFeignClientFallbackFactory implements FallbackFactory<ESFeignClient> {

    @Override
    public ESFeignClient create(final Throwable throwable) {
        throwable.printStackTrace();

        return new ESFeignClient(){

            @Override
            public AjaxResult save(CourseDoc courseDoc) {
                throwable.printStackTrace();
                return AjaxResult.me().setSuccess(false)
                        .setMessage("ES服务不可用["+throwable.getMessage()+"]");
            }

            @Override
            public AjaxResult deleteById(Long id) {
                //System.out.println("ES不可用......托底了...");
                throwable.printStackTrace();
                return AjaxResult.me().setSuccess(false)
                        .setMessage("ES服务不可用["+throwable.getMessage()+"]");
            }

            public PageList<CourseDoc> search(@RequestBody  NativeSearchQueryBuilder nativeSearchQueryBuilder) {
                throwable.printStackTrace();
                return null;
            }

            @Override
            public PageList<CourseDoc> searchString(String json) {
                return null;
            }

            public PageList<CourseDoc> searchBaseQuery(ESBaseQuery esBaseQuery) {
                return null;
            }

            @Override
            public PageList<CourseDoc> searchCourse(@RequestBody CourseQuery courseQuery) {
                throwable.printStackTrace();
                return new PageList();
            }
        };
    }
}
